package com.citygis.securitysystem.system.config;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @ClassName: SwaggerConfig 
 * @Description: TODO
 * @author: liuzg
 * @Date: 2021/1/21 9:38
 * @Version: v1.0
 */

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Value(value = "${swagger.enabled}")
    Boolean swaggerEnabled;

    /**
     * 创建API应用
     * apiInfo() 增加API相关信息
     * 通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现，
     * 本例采用指定扫描的包路径来定义指定要建立API的目录。
     */


//    @Bean
//    @Profile("prod")
//    public Docket createRestApi() {
//        return new Docket(DocumentationType.SWAGGER_2)
//                //.host("10.81.71.93")  //获取请求的主机
//                .apiInfo(apiInfo())
//                .select()
//                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
//                .paths(PathSelectors.any())
//                .build();
//    }
    @Bean
    @Profile("dev")
    public Docket createsRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                //.host("192.168.1.127")  //获取请求的主机
                .apiInfo(apiInfo())
                .enable(swaggerEnabled)
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }


    /**
     * 创建该API的基本信息（这些基本信息会展现在文档页面中）
     * 访问地址：http://localhost:8080/swagger-ui.html
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Spring Security文档")
                .description("接口描述")
                .termsOfServiceUrl("http://code.gefork.com/service")
                .version("1.0")
                .contact(new Contact("liuzg", "http://www.gefork.com", "2396119187@qq.com"))
                .build();
    }
}
